﻿CreateGoogle(160,600,null,null,null,null,null,null,null,null);
var gSortField = "IsFeatured";
var gPageIndex = 1;
var pageCount = 20;
var gLastCount = pageCount;
function pageLoad(sender, args) 
{
    if(typeof(groupList) != "undefined")
    {
        RendGroupList(groupList[1],"myList");
        RendPageNavigator(groupList[0],"divPage","GetPage");
        RendTags(groupList[2],"GroupTagsItem");
        showProgressBar(false);
        groupList = null;
    }
    else
    {
        if(typeof(groupPage) != "undefined")
        {
            RendPageNavigator(groupPage,"divPage","GetPage");
        }
        /*if(typeof(tagList) != "undefined")
        {
            RendTags(tagList,"GroupTagsItem");
        }*/
    }
    //InitData(gSortField,"","","","",1);
}
function FirstReplace()
{
    var domDiv = document.getElementById("myList");
    if(domDiv!=null)
    {
        var sTemp = domDiv.innerHTML;
        sTemp = sTemp.replace(/\$LeaveText\$/gi,LeaveText).replace(/\$JoinText\$/gi,JoinText);//.replace(/\$DeleteText\$/gi,DeleteText);
        sTemp = sTemp.replace(/\$C0007\$/gi,C0007).replace(/\$C0017\$/gi,C0017).replace(/\$C2006\$/gi,C2006);
        sTemp = sTemp.replace(/\$C2007\$/gi,C2007).replace(/\$C2008\$/gi,C2008).replace(/\$C2009\$/gi,C2009);
        domDiv.innerHTML = sTemp;
    }
}

function SortList(sortField,obj)
{
    var nodes = obj.parentNode.childNodes;
    for(var i=0;i<nodes.length;i++)
    {
        if(nodes[i].tagName=="LI")
        {
        if( nodes[i].className.indexOf("first")>-1)
        {
            nodes[i].className = "unSelATags first";
        }else
        {
            nodes[i].className = "unSelATags";
        }
        }
        
    }
     if(sortField=="PMembers")
     {
         obj.className="selATags first";
     }else
     {
        obj.className="selATags";
     }
   
    
    gSortField = sortField;
    if(searVal != "" || learnVal != "" || speakVal != ""||typeVal != "")
    {   
        InitData(gSortField,searVal,learnVal,speakVal,typeVal,1);
    }
    else
    {    
        searDocs();
    }
}
function searDocs()
{
    searVal = "";
    learnVal = "";
    speakVal = "";
    typeVal = "";
    showProgressBar(true);
	
	var tSear = ($get("txtSear")==null?"":$get("txtSear").value);
    var sL = ($get("selLearnL")==null?"": $get("selLearnL").value);
    var sB = ($get("selBaseL")==null?"":$get("selBaseL").value);
    var sGT = ($get("selGroupType")==null?"":$get("selGroupType").value);
	
    InitData(gSortField,tSear,sL,sB,sGT,1);
}
function InitData(sortField,key,learnL,baseLanguage,groupType, pageIndex)
{
	showProgressBar(true); 
  WebServices.Groups.GetAllGroups(sortField,key,learnL,baseLanguage,groupType, pageIndex, pageCount,onsucc);
}
function onsucc(result)
{
   if(Sys.UI.DomElement.containsCssClass($get("divPage"), "DisplayNone"))
   {
       Sys.UI.DomElement.removeCssClass($get("divPage"), "DisplayNone");
   }
   RendGroupList(result[1],"myList");
   RendPageNavigator(result[0],"divPage","GetPage");
   //RendTags(result[2],"GroupTagsItem");
   showProgressBar(false);
}

function GetPage(pageIndex)
{
		showProgressBar(true);
    gPageIndex = pageIndex;
    InitData(gSortField,$get("txtSear").value,$get("selLearnL").value,$get("selBaseL").value,"",pageIndex);
}
function RendGroupList(list,divID)
{
    var domDiv = $get(divID);
    if(domDiv!=null)
    {
        if(Sys.UI.DomElement.containsCssClass(domDiv, "DisplayNone"))
        {
            Sys.UI.DomElement.removeCssClass(domDiv, "DisplayNone");
        }
        if($get("groupItem")==null) return;
        gLastCount = list.length;
        var item= $get("groupItem").innerHTML;
        var listBuilder = new Sys.StringBuilder();
        for (var index = 0; index < list.length; ++index) 
        {
            var obj = list[index];
            var tmp = item.replace(/\$Avatar\$/gi,"<img src=\""+(obj.Avatar==""?"/Images/No_pic.gif":obj.Avatar)+"\"/>").replace(/\$GroupID\$/gi,obj.GroupID).replace(/\$Title\$/gi,obj.Title.replace(/\"/gi,"&quot;")).replace(/\$Title1\$/gi,GetString(obj.Title,50,"..."));
            tmp = tmp.replace(/\$ImgUrlPath\$/gi,"<img src=\""+(obj.ImgUrlPath==""?"/Images/No_pic.gif":obj.ImgUrlPath)+"\"/>");
            tmp = tmp.replace(/\$Language1T\$/gi,obj.Language1==""?"<a href='/groups/grouplist.htm'>"+C0027+"</a>":"<a href='/groups/grouplist.htm?learn="+obj.Language1+"'>"+GetLanguageLabel("selBaseL",obj.Language1)+"</a>").replace(/\$Language2T\$/gi,obj.Language2==""?"<a href='/groups/grouplist.htm'>"+C0027+"</a>":"<a href='/groups/grouplist.htm?speak="+obj.Language2+"'>"+GetLanguageLabel("selBaseL",obj.Language2)+"</a>");
            tmp = tmp.replace(/\$NickName\$/gi,obj.NickName).replace(/\$MemberNo\$/gi,obj.Memberno).replace(/\$Language1\$/gi,obj.Language1).replace(/\$Language2\$/gi,obj.Language2);
            tmp = tmp.replace(/\$members\$/gi,obj.Members).replace(/\$discussions\$/gi,obj.Discussions).replace(/\$Topics\$/gi,obj.Topics);
            tmp = tmp.replace(/\$Walls\$/gi,obj.Walls).replace(/\$GroupType\$/gi,obj.GroupType).replace(/\$Creator\$/gi,obj.Memberno).replace(/\$Desc\$/gi,GetString(obj.Desc,128,"..."));
            tmp = tmp.replace(/\$CreateWho\$/gi,C0093.replace("{0}","<a href='/"+obj.Memberno+".htm'>"+obj.NickName+"</a>"));
            var arr1 = gDateFormat.split(":");
            var arr2 = obj.DiffDate.split(":");
            if(arr2[0]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[0]).replace("{1}",arr1[0]));
            }
            else if(arr2[1]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[1]).replace("{1}",arr1[1]));
            }
            else if(arr2[2]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[2]).replace("{1}",arr1[2]));
            }
            else if(arr2[3]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[3]).replace("{1}",arr1[3]));
            }
            else if(arr2[4]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[4]).replace("{1}",arr1[4]));
            }
            else if(arr2[5]>0)
            {
                tmp = tmp.replace(/\$CreateDate\$/gi,gStrDate.replace("{0}",arr2[5]).replace("{1}",arr1[5]));
            }
            if(obj.GroupType=="3")
            {
				tmp = tmp.replace(/\$GroupT\$/gi,"<A href='/groups/grouplist.htm?type="+obj.GroupType+"'>"+C0087+"</a>");
            }
            else
            {
				if(obj.GroupType=="1")
				{
					tmp = tmp.replace(/\$GroupT\$/gi,"<A href='/groups/grouplist.htm?type="+obj.GroupType+"'>"+GP129+"</a>");
				}
				else
				{
					tmp = tmp.replace(/\$GroupT\$/gi,"<A href='/groups/grouplist.htm?type="+obj.GroupType+"'>"+CT011+"</a>");
				}
            }
            if(obj.GroupType=="3")
            {
				
                tmp = tmp.replace(/\$Lang1\$/gi,"").replace(/\$Lang2\$/gi,C0017+": ");
				tmp = tmp.replace(/\$Display\$/gi,"DisplayNone");
            }
            else
            {
                tmp = tmp.replace(/\$Display\$/gi,"");
				if(obj.GroupType=="1")
				{
					tmp = tmp.replace(/\$Lang1\$/gi,C0016+": ").replace(/\$Lang2\$/gi,"<br />"+C0017+": ");
				}
				else
				{
					
					tmp = tmp.replace(/\$Lang1\$/gi,GP050+": ").replace(/\$Lang2\$/gi,", ");
				}
			}
            
            if(Memberno!=""&&Memberno==obj.Memberno)
            {
                /*tmp = tmp.replace(/\$JoinImg\$/gi,"<img src=\"/Images/icon_res_delete2.gif\" align=\"absmiddle\" />").replace(/\$JoinText\$/gi,DeleteText);
                tmp = tmp.replace(/\$JoinFunc\$/gi,"DeleteGroup('"+obj.GroupID+"',this)");*/
                tmp = tmp.replace(/\$JoinImg\$/gi,"").replace(/\$JoinText\$/gi,"").replace(/\$JoinFunc\$/gi,"");
            }
            else if(Memberno!=""&&HaveGroupID(obj.GroupID))
            {
                tmp = tmp.replace(/\$JoinImg\$/gi,"<img src=\"/Images/icon_group_leave.gif\" align=\"absmiddle\" />").replace(/\$JoinText\$/gi,LeaveText);
                tmp = tmp.replace(/\$JoinFunc\$/gi,"LeaveGroup('"+obj.GroupID+"',this)");
            }
            else
            {
                tmp = tmp.replace(/\$JoinImg\$/gi,"<img src=\"/Images/icon_group_join.gif\" align=\"absmiddle\" />").replace(/\$JoinText\$/gi,JoinText);
                tmp = tmp.replace(/\$JoinFunc\$/gi,"JoinGroup('"+obj.GroupID+"','"+escape(obj.Title)+"','"+obj.JoinOptions+"','"+obj.Memberno+"',this)");
            }
			
            listBuilder.append(tmp);
        }
        var sTemp = listBuilder.toString();
        sTemp = sTemp.replace(/\$C0007\$/gi,C0007).replace(/\$C0017\$/gi,C0017).replace(/\$C2006\$/gi,C2006);
        sTemp = sTemp.replace(/\$C2007\$/gi,C2007).replace(/\$C2008\$/gi,C2008).replace(/\$C2009\$/gi,C2009);
        domDiv.innerHTML = sTemp;
    }
}
function HaveFreinds(member)
{
    var flag = false;
    if(myFreindsList)
    {
        for(var i=0;i<myFreindsList.length;i++)
        {
            if(myFreindsList[i]==member)
            {
                flag = true;
                break;
            }
        }
    }
    return flag;
}
function JoinGroup(GroupID,groupTitle,JoinOptions,creator,e)
{
    if(!Memberno||Memberno=="")
    {
        top.location = "/user/login.htm?IsShow=1&ReturnUrl="+top.location.href;
        return;
    }
    if(JoinOptions=="2"&&HaveFreinds(creator)) JoinOptions="1";
    
    if(JoinOptions=="1")
    {
        Sys.UI.DomElement.removeCssClass($get("JoinPanel1"), "DisplayNone");
        $create(AjaxControlToolkit.ModalPopupBehavior, 
        {
            "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton1","OnCancelScript":"onCancel('ModalPopup1')",
            "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton1","OnOkScript":"onOk('ModalPopup1','"+GroupID+"')",
            "PopupControlID":"JoinPanel1","PopupDragHandleControlID":"JoinPanelTitle1","id":"ModalPopup1"
        }, null, null, e);
        $find("ModalPopup1").show();
    }
    else if(JoinOptions=="2")
    {
        
        Sys.UI.DomElement.removeCssClass($get("JoinPanel2"), "DisplayNone");
        $create(AjaxControlToolkit.ModalPopupBehavior, 
        {
            "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton2","OnCancelScript":"onCancel('ModalPopup2')",
            "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton2","OnOkScript":"onOk('ModalPopup2','"+GroupID+"')",
            "PopupControlID":"JoinPanel2","PopupDragHandleControlID":"JoinPanelTitle2","id":"ModalPopup2"
        }, null, null, e);
        $find("ModalPopup2").show();
    }
     else if(JoinOptions=="3")
    {
        Sys.UI.DomElement.removeCssClass($get("JoinPanel3"), "DisplayNone");
        $create(AjaxControlToolkit.ModalPopupBehavior, 
        {
            "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton3","OnCancelScript":"onCancel('ModalPopup3')",
            "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton3","OnOkScript":"onOk('ModalPopup3','"+GroupID+"')",
            "PopupControlID":"JoinPanel3","PopupDragHandleControlID":"JoinPanelTitle3","id":"ModalPopup3"
        }, null, null, e);
        $find("ModalPopup3").show();
    }
}
function onOk(eid,groupid)
{
    var cmp = $find(eid);
    if(cmp) $get(cmp.get_OkControlID()).disabled = true;
    if(eid=="ModalPopup1")
    {
        WebServices.Groups.JoinGroup(groupid,Memberno,onsuccOP,null,cmp);
    }
    else if(eid=="ModalPopup2")
    {
       WebServices.Groups.JoinGroup1(groupid,Memberno,$get("msg1").value,onsuccOP,null,cmp);
       $get("msg1").value = "";
    }
    else if(eid=="ModalPopup3")
    {
       WebServices.Groups.JoinGroup1(groupid,Memberno,$get("msg2").value,onsuccOP,null,cmp);
       $get("msg2").value = "";
    }
    else if(eid=="ModalPopup4")
    {
        WebServices.Groups.LeaveGroup(groupid,Memberno,onsuccOP,null,cmp);
    }
    /*else  if(eid=="ModalPopup5")
    {
        WebServices.Groups.DeleteGroup(groupid,Memberno,onsuccOP,null,cmp);
    }*/
}

function onsuccOP(result,cmp)
{
    if(result.State) 
    {   
        if(cmp)
        {
            if(cmp._id=="ModalPopup1")
            {
                jionGroupList[jionGroupList.length] = result.Msg;
            }
            else if(cmp._id=="ModalPopup4")
            {
                if(jionGroupList)
                {
                    var k = 0;
                    for(var i=0;i<jionGroupList.length;i++)
                    {
                        if(jionGroupList[i]==result.Msg)
                        {
                            k = i;
                            break;
                        }
                    }
                    for(;k<jionGroupList.length-1;k++)
                    {
                        jionGroupList[k] = jionGroupList[k+1];
                    }
                    jionGroupList[jionGroupList.length-1] = null;
                }
            }
            else if(cmp._id=="ModalPopup5")
            {
                if(gLastCount==1) gPageIndex = gPageIndex -1;
                if(gPageIndex<1) gPageIndex = 1;
            }
            DisposeModal(cmp);
        }
        InitData(gSortField,$get("txtSear").value,$get("selLearnL").value,$get("selBaseL").value,"",gPageIndex);
        alert(joinsucc);
    }
    else
    {
        if(cmp) DisposeModal(cmp);
        alert(joinfail);
    }
}
function onCancel(eid)
{
    DisposeModal(eid);
}
function DisposeModal(eid)
{
    var cmp = eid;
    if (typeof eid == 'string')
    {
        cmp = $find(eid);
    }
    if(cmp)
    {
        var PopupControlID = cmp.get_PopupControlID();
         $get(cmp.get_OkControlID()).disabled = false;
        cmp.dispose();
        Sys.UI.DomElement.addCssClass($get(PopupControlID), "DisplayNone");
    }
}
function RendTags(list,divID)
{
    var domDiv = $get(divID);
    if(domDiv!=null)
    {
        if(Sys.UI.DomElement.containsCssClass(domDiv, "DisplayNone"))
        {
            Sys.UI.DomElement.removeCssClass(domDiv, "DisplayNone");
        }
        var item= "<li><a href=\"/groups/grouplist.htm?tag=$Tag$\">$Tag$</a></li>";
        var listBuilder = new Sys.StringBuilder();
        listBuilder.append("<ul class=''>");
        for (var index = 0; index < list.length; ++index) 
        {
        		var tmptag = list[index];
        		var i = tmptag.lastIndexOf("(");
        		var tmptag1 = tmptag;
        		if(i>0)
        		{
        			tmptag1 = tmptag.substr(0,i);
        		}
            listBuilder.append(item.replace(/\$Tag\$/gi,tmptag).replace(/\$Tag1\$/gi,tmptag1));
        }
        listBuilder.append("</ul><div class=\"clear\"></div>");
        domDiv.innerHTML = listBuilder.toString();
    }
}
function HaveGroupID(groupID)
{
    var flag = false;
    if(jionGroupList)
    {
        for(var i=0;i<jionGroupList.length;i++)
        {
            if(jionGroupList[i]==groupID)
            {
                flag = true;
                break;
            }
        }
    }
    return flag;
}
function LeaveGroup(GroupID,e)
{
    Sys.UI.DomElement.removeCssClass($get("LeavePanel"), "DisplayNone");
    $create(AjaxControlToolkit.ModalPopupBehavior, 
    {
        "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton4","OnCancelScript":"onCancel('ModalPopup4')",
        "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton4","OnOkScript":"onOk('ModalPopup4','"+GroupID+"')",
        "PopupControlID":"LeavePanel","PopupDragHandleControlID":"LeavePanelTitle","id":"ModalPopup4"
    }, null, null, e);
    $find("ModalPopup4").show();
}
/*function DeleteGroup(GroupID,e)
{
    Sys.UI.DomElement.removeCssClass($get("DeletePanel"), "DisplayNone");
    $create(AjaxControlToolkit.ModalPopupBehavior, 
    {
        "BackgroundCssClass":"modalBackground","CancelControlID":"CancelButton5","OnCancelScript":"onCancel('ModalPopup5')",
        "DropShadow":false,"DynamicServicePath":null,"OkControlID":"OkButton5","OnOkScript":"onOk('ModalPopup5','"+GroupID+"')",
        "PopupControlID":"DeletePanel","PopupDragHandleControlID":"DeletePanelTitle","id":"ModalPopup5"
    }, null, null, e);
    $find("ModalPopup5").show();
}*/
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();
